<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="Taskname"
      ></el-input>
      <el-button @click="delTask">清除</el-button>
      <el-button type="primary" @click="searchList">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            size="mini"
            style="margin-right: 10px"
            @click="resiveList(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delList(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <!-- 分页功能 -->
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="params._limit"
        @current-change="changePage"
      >
      </el-pagination>
      <el-button size="small" plain @click="showDia">+ 新增</el-button>
    </div>
    <!-- 弹框位置 -->
    <el-dialog
      :visible="dialogVisible"
      width="30%"
      title="提示"
      @close="closeDia"
    >
      <el-form ref="form" :model="addForm" label-width="80px" :rules="rules">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="addForm.des"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  IncreaseTask,
  deleteTask,
  ReviseTask,
  getTask,
  searchTask,
} from "@/api/api";
import { v4 as uuidv4 } from "uuid";
export default {
  data() {
    return {
      list: [],
      addForm: {
        name: "",
        des: "",
        // id: null,
      },
      // id: uuidv4(),
      params: {
        _page: 1,
        _limit: 5,
      },
      total: null,
      dialogVisible: false,
      Taskname: "",

      rules: {
        name: [{ required: true, message: "任务名称必填", trigger: "blur" }],
        des: [{ required: true, message: "任务必填", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.get();
  },
  methods: {
    async get() {
      const res = await getList(this.params);
      this.list = res.data;
      console.log(this.list);
      this.total = Number(res.headers["x-total-count"]);
    },
    //翻页显示
    changePage(page) {
      this.params._page = page;
      //渲染列表
      this.get();
    },
    showDia() {
      this.dialogVisible = true;
    },
    closeDia() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
      this.addForm = {
        name: "",
        des: "",
        id: "",
      };
    },
    confirm() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return;
        //新增还是编辑
        if (this.addForm.id) {
          await ReviseTask(this.addForm);
        } else {
          await IncreaseTask({ ...this.addForm, id: uuidv4() });
        }
        //提示新增成功
        this.$message.success(`${this.addForm.id ? "编辑" : "新增"}成功`);
        // //重置表单
        // this.$refs.form.resetFields();
        // this.resstFrom();
        //重新渲染列表
        this.get();
        // 关闭弹框
        this.closeDia();
      });
    },

    //删除
    async delList(id) {
      await deleteTask(id);
      // 渲染表单
      this.get();
    },
    // 回显
    async resiveList(id) {
      const res = await getTask(id);
      this.showDia();
      this.addForm = res.data;
    },
    //搜索
    async searchList() {
      const res = await searchTask({ name: this.Taskname });
      console.log(res);
      this.list = res.data;
    },
    //清除
    delTask() {
      this.Taskname = "";
      //渲染列表
      this.get();
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
